<template>
  <div class="app-container">
    <!--查询表单-->
    <div class="search-div">
      <el-form label-width="70px" size="small">
        <el-row>
          <el-col :span="8">
            <el-form-item label="房屋名称">
              <el-input
                style="width: 30%"
                v-model="searchObj.name"
                placeholder="房屋门牌"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="创建时间">
              <el-date-picker
                v-model="createTimes"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                style="margin-right: 10px; width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display: flex">
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="fetchData()"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetData"
            >重置</el-button
          >
        </el-row>
      </el-form>
    </div>

    <!-- 工具条 -->
    <div class="tools-div">
      <el-button type="success" icon="el-icon-plus" size="mini" @click="add"
        >添 加</el-button
      >
    </div>

    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      stripe
      border
      style="width: 100%; margin-top: 10px"
    >
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <!-- <el-table-column prop="communityName" label="小区名称" /> -->
      <!-- <el-table-column prop="communityId" label="小区id" /> -->
      <el-table-column prop="buildingName" label="楼栋名称" />
      <!-- <el-table-column prop="buildingId" label="楼栋id" /> -->
      <el-table-column prop="code" label="房屋编号" />
      <el-table-column prop="name" label="房屋名称" />
      <el-table-column prop="ownerId" label="业主id"> </el-table-column>
      <el-table-column prop="ownerName" label="业主姓名" />
      <el-table-column prop="telephone" label="联系方式" />
      <el-table-column prop="roomNum" label="房间数" />
      <el-table-column prop="unit" label="单元" />
      <el-table-column prop="floor" label="楼层" />
      <el-table-column prop="description" label="描述" />
      <el-table-column prop="liveTime" label="入住时间" />
      <el-table-column prop="updateTime" label="创建时间" />
      <el-table-column prop="updateTime" label="更新时间" />
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="edit(scope.row.id)"
            title="修改"
          />
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="removeDataById(scope.row.id)"
            title="删除"
          />
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加/修改" :visible.sync="dialogVisible" width="40%">
      <el-form
        ref="dataForm"
        :model="sysHouse"
        label-width="150px"
        size="small"
        style="padding-right: 40px"
      >
        <el-form-item label="所属小区" prop="communityName">
          <el-select v-model="sysHouse.communityName"  @change="handleCommunityChange"  placeholder="请选择小区">
            <el-option
              v-for="item in communityList"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属楼栋" prop="buildingName">
          <el-select v-model="sysHouse.buildingName"   @change="handleBuildChange"  placeholder="请选择楼栋">
            <el-option v-for="item in buildList" :key="item.id"
            :label="item.name"
            :value="item.name"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="楼栋名称">
          <el-input v-model="sysHouse.buildingName" />
        </el-form-item> -->
        <el-form-item label="房屋名称">
          <el-input v-model="sysHouse.name" />
        </el-form-item>
        <el-form-item label="业主姓名">
          <el-input v-model="sysHouse.ownerName" />
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input v-model="sysHouse.telephone" />
        </el-form-item>
        <el-form-item label="房间数量">
          <el-input v-model="sysHouse.roomNum" />
        </el-form-item>
        <el-form-item label="单元">
          <el-input v-model="sysHouse.unit" />
        </el-form-item>
        <el-form-item label="楼层">
          <el-input v-model="sysHouse.floor" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="sysHouse.description" />
        </el-form-item>
        <!-- <el-form-item label="入住时间">
            <el-date-picker v-model="sysHouse.liveTime" />
          </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button
          @click="dialogVisible = false"
          size="small"
          icon="el-icon-refresh-right"
          >取 消</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-check"
          @click="saveOrUpdate()"
          size="small"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />
  </div>
</template>
     
     <script>
import api from "@/api/sysHouse/house";

import communityApi from "@/api/sysCommunity/community";

import buildApi from "@/api/sysBuild/build"
export default {
  data() {
    return {
      listLoading: true,
      list: [],
      total: 0,
      page: 1,
      limit: 5,
      createTimes: [],
      searchObj: {},
      sysHouse: {},
      dialogVisible: false,

      //小区列表
      communityList: [],
      //楼栋列表
      buildList: [],
    };
  },
  created() {
    this.fetchData(1);
    this.finAllCommunity();
    this.finAllBuild();
  },
  methods: {
    finAllCommunity() {
      communityApi.finAllCommunity().then((res) => {
        this.communityList = res.data;
      });
    },
    finAllBuild() {
      buildApi.finAllBuild().then((res) => {
        this.buildList = res.data;
      });
    },

    handleCommunityChange(value) {
    // 根据选中的小区名称获取对应的小区ID，并赋值给sysBuild.communityId
    const selectedCommunity = this.communityList.find(item => item.name === value);
    console.log(selectedCommunity);
    if (selectedCommunity) {
      this.sysHouse.communityId = selectedCommunity.id;
    }
  },
  handleBuildChange(value) {
    // 根据选中的小区名称获取对应的小区ID，并赋值给sysBuild.communityId
    const selectedBuild = this.buildList.find(item => item.name === value);
    console.log(selectedBuild);
    if (selectedBuild) {
      this.sysHouse.buildingId = selectedBuild.id;
    }
  },
    //得到数据
    fetchData(page = 1) {
      // console.log(this.createTimes);
      this.page = page;
      if (this.createTimes && this.createTimes.length == 2) {
        this.searchObj.createTimeBegin = this.createTimes[0];
        this.searchObj.createTimeEnd = this.createTimes[1];
        // console.log(this.searchObj.createTimeBegin);
      }
      api.getPageList(this.page, this.limit, this.searchObj).then((res) => {
        this.list = res.data.records;
        this.total = res.data.total;
        this.listLoading = false;
      });
    },

    //重置按钮
    resetData() {
      // 清空表单
      this.searchObj = {};

      //清空日期条件
      this.createTimes = [];

      // 再次刷新列表数据
      this.fetchData();
    },
    // //修改按钮
    edit(id) {
      api.getHouseById(id).then((res) => {
        // console.log(res)
        this.sysHouse = res.data;
        //弹出弹框
        this.dialogVisible = true;
      });
    },
    //添加按钮
    add() {
      this.sysHouse = {};
      this.dialogVisible = true;
    },
    removeDataById(id) {
      this.$confirm("此操作将永久删除该房屋信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        // 点击确定
        api.removeById(id).then((response) => {
          // 1.给出提示
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.listLoading = true;
          //2. 再次刷新数据列表
          this.fetchData();
        });
      });
    },
    //保存/修改
    saveOrUpdate() {
      if (this.sysHouse.id) {
        //修改
        this.update();
      } else {
        this.addHouse();
      }
    },

    update() {
      api.updateHouse(this.sysHouse).then((res) => {
        // 1.给出提示
        this.$message({
          type: "success",
          message: "修改成功!",
        });
        //刷新列表
        this.fetchData();
        //关闭弹框
        this.dialogVisible = false;
      });
    },

    addHouse() {
      console.log("添加前");
      api.addHouse(this.sysHouse).then((res) => {
        // 1.给出提示
        this.$message({
          type: "success",
          message: "添加成功!",
        });
        //刷新列表
        this.fetchData();
        //关闭弹框
        this.dialogVisible = false;
      });
    },
  },
};
</script>
     
     <style>
</style>